#include "raymarcher\src\glsl\randTable.glsl"

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
out vec4 OutColor;

#define LATTICE_BIAS 0.01


// The Psedudo random number generator
float prns(vec2 coord){
    return fract(sin(dot(coord.xy,vec2(12.9898,78.233)))*43758.5453);
}

float GridLatticeNoise(vec2 p){
    float value=prns(ceil(p+vec2(0.)));
    value+=prns(ceil(p+vec2(LATTICE_BIAS,0.)));
    value+=prns(ceil(p+vec2(LATTICE_BIAS,LATTICE_BIAS)));
    value+=prns(ceil(p+vec2(-LATTICE_BIAS,0.)));
    value+=prns(ceil(p+vec2(-LATTICE_BIAS,-LATTICE_BIAS)));
    value+=prns(ceil(p+vec2(0.,-LATTICE_BIAS)));
    value+=prns(ceil(p+vec2(0.,LATTICE_BIAS)));
    value+=prns(ceil(p+vec2(LATTICE_BIAS,-LATTICE_BIAS)));
    value+=prns(ceil(p+vec2(-LATTICE_BIAS,LATTICE_BIAS)));
    value/=9.;
    return value;
}


float SimpleGridNoise(vec2 p) {
    vec2 bf = fract(p);
    vec2 bi = p - bf;
    
    return randTable[int(mod(dot(bi, bi), 256))];
}

void main(){
    vec2 uv=gl_FragCoord.xy/u_resolution;
    
    vec2 p=uv*vec2(70.)+vec2(5.,0.)*(u_time);
    p.x*=u_resolution.x/u_resolution.y;
    
    // float value=GridLatticeNoise(p);
    float value=SimpleGridNoise(p);
    
    vec3 col=vec3(value);
    
    col=pow(col,vec3(1./2.2));
    
    OutColor=vec4(col,1.);
}